import javax.swing.tree.TreeNode;

public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "key=" + key +
                    '}';
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode parent = null;
        TreeNode rt = root;
        while (rt != null) {
            if (key < rt.key) {
                parent = rt;
                rt = rt.left;
            }else if (key > rt.key) {
                parent = rt;
                rt = rt.right;
            }else {
                return false;//二叉搜索树不能有相同的key
            }
        }
        if (key < parent.key) {
            parent.left = new TreeNode(key);
        }else {
            parent.right = new TreeNode(key);
        }

        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode rt = root;
        while (rt != null) {
            if (rt.key > key) {
                rt = rt.left;
            }else if (rt.key < key) {
                rt = rt.right;
            }else {
                return rt;
            }
        }
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.key < key) {
                parent = cur;
                cur = cur.right;
            }else if (cur.key > key) {
                parent = cur;
                cur = cur.left;
            }else {//找到了
                removeFun(parent,cur);
                return true;
            }
        }
        return false;
    }
    private void removeFun(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {//1.cur 的左边为空
            if (root == cur) {
                root = cur.right;
            }else if (cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null) {//2.cur 的右边为空
            if (root == cur) {
                root = cur.left;
            }else if (cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {//3.cur 的左右都不为空
            TreeNode t = cur.right;
            TreeNode tp = cur;
            while (t.left != null) {//找到要替换 被删除节点 的节点
                tp = t;
                t = t.left;
            }
            cur.key = t.key;
            if (tp.left == t) {//左子树时
                tp.left = t.right;
            }else {//右子树时
                tp.right = t.right;
            }
        }
    }
}
